1. Field of the Invention
The present invention is directed to memory management. It particularly concerns what has come to be known as xe2x80x9cgarbage collection.xe2x80x9d
2. Background Information
In the field of computer systems, considerable effort has been expended on the task of allocating memory to data objects. For the purposes of this discussion, the term object refers to a data structure represented in a computer system""s memory. Other terms sometimes used for the same concept are record and structure. An object may be identified by a reference, a relatively small amount of information that can be used to access the object. A reference can be represented as a xe2x80x9cpointerxe2x80x9d or a xe2x80x9cmachine address,xe2x80x9d which may require, for instance, only sixteen, thirty-two, or sixty-four bits of information, although there are other ways to represent a reference.
In some systems, which are usually known as xe2x80x9cobject oriented,xe2x80x9d objects may have associated methods, which are routines that can be invoked by reference to the object. They also may belong to a class, which is an organizational entity that may contain method code or other information shared by all objects belonging to that class. In the discussion that follows, though, the term object will not be limited to such structures; it will additionally include structures with which methods and classes are not associated.
The invention to be described below is applicable to systems that allocate memory to objects dynamically. Not all systems employ dynamic allocation. In some computer languages, source programs can be so written that all objects to which the program""s variables refer are bound to storage locations at compile time. This storage-allocation approach, sometimes referred to as xe2x80x9cstatic allocation,xe2x80x9d is the policy traditionally used by the Fortran programming language, for example.
Even for compilers that are thought of as allocating objects only statically, of course, there is often a certain level of abstraction to this binding of objects to storage locations. Consider the typical computer system 10 depicted in FIG. 1, for example. Data, and instructions for operating on them, that a microprocessor 11 uses may reside in on-board cache memory or be received from further cache memory 12, possibly through the mediation of a cache controller 13. That controller 13 can in turn receive such data from system read/write memory (xe2x80x9cRAMxe2x80x9d) 14 through a RAM controller 15 or from various peripheral devices through a system bus 16. The memory space made available to an application program may be xe2x80x9cvirtualxe2x80x9d in the sense that it may actually be considerably larger than RAM 14 provides. So the RAM contents will be swapped to and from a system disk 17.
Additionally, the actual physical operations performed to access some of the most-recently visited parts of the process""s address space often will actually be performed in the cache 12 or in a cache on board microprocessor 11 rather than on the RAM 14, with which those caches swap data and instructions just as RAM 14 and system disk 17 do with each other.
A further level of abstraction results from the fact that an application will often be run as one of many processes operating concurrently with the support of an underlying operating system. As part of that system""s memory management, the application""s memory space may be moved among different actual physical locations many times in order to allow different processes to employ shared physical memory devices. That is, the location specified in the application""s machine code may actually result in different physical locations at different times because the operating system adds different offsets to the machine-language-specified location.
Despite these expedients, the use of static memory allocation in writing certain long-lived applications makes it difficult to restrict storage requirements to the available memory space. Abiding by space limitations is easier when the platform provides for dynamic memory allocation, i.e., when memory space to be allocated to a given object is determined only at run time.
Dynamic allocation has a number of advantages, among which is that the run-time system is able to adapt allocation to run-time conditions. For example, the programmer can specify that space should be allocated for a given object only in response to a particular run-time condition. The C-language library function malloc( ) is often used for this purpose. Conversely, the programmer can specify conditions under which memory previously allocated to a given object can be reclaimed for reuse. The C-language library function free( ) results in such memory reclamation.
Because dynamic allocation provides for memory reuse, it facilitates generation of large or long-lived applications, which over the course of their lifetimes may employ objects whose total memory requirements would greatly exceed the available memory resources if they were bound to memory locations statically.
Particularly for long-lived applications, though, allocation and reclamation of dynamic memory must be performed carefully. If the application fails to reclaim unused memoryxe2x80x94or, worse, loses track of the address of a dynamically allocated segment of memoryxe2x80x94its memory requirements will grow over time to exceed the system""s available memory. This kind of error is known as a xe2x80x9cmemory leak.xe2x80x9d
Another kind of error occurs when an application reclaims memory for reuse even though it still maintains a reference to that memory. If the reclaimed memory is reallocated for a different purpose, the application may inadvertently manipulate the same memory in multiple inconsistent ways. This kind of error is known as a xe2x80x9cdangling reference,xe2x80x9d because an application should not retain a reference to a memory location once that location is reclaimed. Explicit dynamic-memory management by using interfaces like malloc( )/free( ) often leads to these problems.
A way of reducing the likelihood of such leaks and related errors is to provide memory-space reclamation in a more-automatic manner. Techniques used by systems that reclaim memory space automatically are commonly referred to as xe2x80x9cgarbage collection.xe2x80x9d Garbage collectors operate by reclaiming space that they no longer consider xe2x80x9creachable.xe2x80x9d Statically allocated objects represented by a program""s global variables are normally considered reachable throughout a program""s life. Such objects are not ordinarily stored in the garbage collector""s managed memory space, but they may contain references to dynamically allocated objects that are, and such objects are considered reachable. Clearly, an object referred to in the processor""s call stack is reachable, as is an object referred to by register contents. And an object referred to by any reachable object is also reachable.
The use of garbage collectors is advantageous because, whereas a programmer working on a particular sequence of code can perform his task creditably in most respects with only local knowledge of the application at any given time, memory allocation and reclamation require a global knowledge of the program. Specifically, a programmer dealing with a given sequence of code does tend to know whether some portion of memory is still in use for that sequence of code, but it is considerably more difficult for him to know what the rest of the application is doing with that memory. By tracing references from some conservative notion of a xe2x80x9croot set,xe2x80x9d e.g., global variables, registers, and the call stack, automatic garbage collectors obtain global knowledge in a methodical way. By using a garbage collector, the programmer is relieved of the need to worry about the application""s global state and can concentrate on local-state issues, which are more manageable. The result is applications that are more robust, having no dangling references and fewer memory leaks.
Garbage-collection mechanisms can be implemented by various parts and levels of a computing system. One approach is simply to provide them as part of a batch compiler""s output. Consider FIG. 2""s simple batch-compiler operation, for example. A computer system executes in accordance with compiler object code and therefore acts as a compiler 20. The compiler object code is typically stored on a medium such as FIG. 1""s system disk 17 or some other machine-readable medium, and it is loaded into RAM 14 to configure the computer system to act as a compiler. In some cases, though, the compiler object code""s persistent storage may instead be provided in a server system remote from the machine that performs the compiling. The electrical signals that carry the digital data by which the computer systems exchange that code are exemplary forms of carrier waves transporting the information.
The input to the compiler is the application source code, and the end product of the compiler process is application object code. This object code defines an application 21, which typically operates on input such as mouse clicks, etc., to generate a display or some other type of output. This object code implements the relationship that the programmer intends to specify by his application source code. In one approach to garbage collection, the compiler 20, without the programmer""s explicit direction, additionally generates code that automatically reclaims unreachable memory space.
Even in this simple case, though, there is a sense in which the application does not itself provide the entire garbage collector. Specifically, the application will typically call upon the underlying operating system""s memory-allocation functions. And the operating system may in turn take advantage of various hardware that lends itself particularly to use in garbage collection. So even a very simple system may disperse the garbage-collection mechanism over a number of computer-system layers.
To get some sense of the variety of system components that can be used to implement garbage collection, consider FIG. 3""s example of a more complex way in which various levels of source code can result in the machine instructions that a processor executes. In the FIG. 3 arrangement, the human applications programmer produces source code 22 written in a high-level language. A compiler 23 typically converts that code into xe2x80x9cclass files.xe2x80x9d These files include routines written in instructions, called xe2x80x9cbyte codesxe2x80x9d 24, for a xe2x80x9cvirtual machinexe2x80x9d that various processors can be configured to emulate. This conversion into byte codes is almost always separated in time from those codes"" execution, so FIG. 3 divides the sequence into a xe2x80x9ccompile-time environmentxe2x80x9d 25 separate from a xe2x80x9crun-time environmentxe2x80x9d 26, in which execution occurs. One example of a high-level-language environment for which compilers are available to produce such virtual-machine instructions is the Java(trademark) platform. (Java is a trademark or registered trademark of Sun Microsystems, Inc., in the Unites States and other countries.)
Most typically, the class files"" byte-code routines are executed by a processor under control of a virtual-machine process 27. That process emulates a virtual machine from whose instruction set the byte codes are drawn. As is true of the compiler 23, the virtual-machine process 27 may be specified by code stored on a local disk or some other machine-readable medium from which it is read into FIG. 1""s RAM 14 to configure the computer system to act as a virtual machine and implement the garbage collector. Again, though, that code""s persistent storage may instead be provided by a server system remote from the processor that implements the virtual machine, in which case the code would be transmitted electrically or optically to the virtual-machine-implementing processor.
In most implementations, much of the virtual machine""s action in executing these byte codes is most like what those skilled in the art refer to as xe2x80x9cinterpreting,xe2x80x9d and FIG. 3 shows that the virtual machine includes an xe2x80x9cinterpreterxe2x80x9d 28 for that purpose. The resultant instructions typically invoke calls to a run-time system 29, which handles matters such as loading new class files as they are needed and, of particular interest in the present connection, performing garbage collection.
In addition to running an interpreter, many virtual-machine implementations also actually compile the byte codes concurrently with the resultant object code""s execution, so FIG. 3 depicts the virtual machine as additionally including a xe2x80x9cjust-in-timexe2x80x9d compiler 30.
The arrangement of FIG. 3 differs from FIG. 2 in that the compiler 23 for converting the human programmer""s code does not contribute to providing the garbage-collection function; that results largely from the virtual machine 27""s operation. Although the FIG. 3 arrangement is a popular one, it is by no means universal, and many further implementation types can be expected. Proposals have even been made to implement the virtual machine 27""s behavior in a hardware processor, in which case the hardware itself would provide some or all of the garbage-collection function.
In short, garbage collectors can be implemented in a wide range of combinations of hardware and/or software. As is true of most of the garbage-collection techniques described in the literature, the invention to be described below is applicable to most such systems.
By implementing garbage collection, a computer system can greatly reduce the occurrence of memory leaks and other software deficiencies in which human programming frequently results. But it can also have significant adverse performance effects if it is not implemented carefully. To distinguish the part of the program that does xe2x80x9cusefulxe2x80x9d work from that which does the garbage collection, the term mutator is sometimes used in discussions of these effects; from the collector""s point of view, what the mutator does is mutate active data structures"" connectivity.
Some garbage-collection approaches rely heavily on interleaving garbage-collection steps among mutator steps. In one type of garbage-collection approach, for instance, the mutator operation of writing a reference is followed immediately by garbage-collector steps used to maintain a reference count in that object""s header, and code for subsequent new-object storage includes steps for finding space occupied by objects whose reference count has fallen to zero. Obviously, such an approach can slow mutator operation significantly.
Other approaches therefore interleave very few garbage-collector-related instructions into the main mutator process but instead interrupt it from time to time to perform garbage-collection cycles, in which the garbage collector finds unreachable objects and reclaims their memory space for reuse. Such an approach will be assumed in discussing FIG. 4""s depiction of a simple garbage-collection operation. Within the memory space allocated to a given application is a part 40 managed by automatic garbage collection. In the following discussion, this will be referred to as the xe2x80x9cheap,xe2x80x9d although in other contexts that term refers to all dynamically allocated memory. During the course of the application""s execution, space is allocated for various objects 42, 44, 46, 48, and 50. Typically, the mutator allocates space within the heap by invoking the garbage collector, which at some level manages access to the heap. Basically, the mutator asks the garbage collector for a pointer to a heap region where it can safely place the object""s data. The garbage collector keeps track of the fact that the thus-allocated region is occupied. It will refrain from allocating that region in response to any other request until it determines that the mutator no longer needs the region allocated to that object.
Garbage collectors vary as to which objects they consider reachable and unreachable. For the present discussion, though, an object will be considered xe2x80x9creachablexe2x80x9d if it is referred to, as object 42 is, by a reference in the root set 52. The root set consists of reference values stored in the mutator""s threads"" call stacks, the CPU registers, and global variables outside the garbage-collected heap. An object is also reachable if it is referred to, as object 46 is, by another reachable object (in this case, object 42). Objects that are not reachable can no longer affect the program, so it is safe to re-allocate the memory spaces that they occupy.
A typical approach to garbage collection is therefore to identify all reachable objects and reclaim any previously allocated memory that the reachable objects do not occupy. A typical garbage collector may identify reachable objects by tracing references from the root set 52. For the sake of simplicity, FIG. 4 depicts only one reference from the root set 52 into the heap 40. (Those skilled in the art will recognize that there are many ways to identify references, or at least data contents that may be references.) The collector notes that the root set points to object 42, which is therefore reachable, and that reachable object 42 points to object 46, which therefore is also reachable. But those reachable objects point to no other objects, so objects 44, 48, and 50 are all unreachable, and their memory space may be reclaimed.
To avoid excessive heap fragmentation, some garbage collectors additionally relocate reachable objects. FIG. 5 shows a typical approach. The heap is partitioned into two halves, hereafter called xe2x80x9csemi-spaces.xe2x80x9d For one garbage-collection cycle, all objects are allocated in one semi-space 54, leaving the other semi-space 56 free. When the garbage-collection cycle occurs, objects identified as reachable are xe2x80x9cevacuatedxe2x80x9d to the other semi-space 56, so all of semi-space 54 is then considered free. Once the garbage-collection cycle has occurred, all new objects are allocated in the lower semi-space 56 until yet another garbage-collection cycle occurs, at which time the reachable objects are evacuated back to the upper semi-space 54.
Although this relocation requires the extra steps of copying the reachable objects and updating references to them, it tends to be quite efficient, since most new objects quickly become unreachable, so most of the current semi-space is actually garbage. That is, only a relatively few, reachable objects need to be relocated, after which the entire semi-space contains only garbage and can be pronounced free for reallocation.
In one sense, the approach of interrupting the mutator occasionally for garbage collection can increase an application""s responsiveness, because the main mutator operation ordinarily proceeds relatively unburdened by garbage-collection overhead. In interactive systems, moreover, interruptions for garbage collection can sometimes be scheduled opportunistically so as to reduce the like-lihood that they will result in much overall speed reduction. Garbage-collection an be triggered when the system is waiting for user input, for instance.
So it may often be true that the garbage-collection operation""s effect on performance can depend less on the total collection time than on when collections actually occur. But another factor that often is even more determinative is the duration of any single collection cycle, i.e., how long the mutator must remain quiescent at any one time. In an interactive system, for instance, a user may never notice hundred-millisecond interruptions for garbage collection, whereas most users would find interruptions lasting for two seconds to be annoying. Many garbage collectors therefore operate incrementally. That is, they perform less than a complete collection in any single interruption of the main application.
A way of not only reducing collection-cycle length but also increasing overall efficiency is to segregate the heap into one or more parts, called generations, that are subject to different collection policies. New objects are allocated in a xe2x80x9cyoungxe2x80x9d generation, and older objects are promoted from younger generations to older or more xe2x80x9cmaturexe2x80x9d generations. Collecting the younger generations more frequently than the others yields greater efficiency because the younger generations tend to accumulate garbage faster; newly allocated objects tend to xe2x80x9cdie,xe2x80x9d while older objects tend to xe2x80x9csurvive.xe2x80x9d
But generational collection greatly increases what is effectively the root set for a given generation. Consider FIG. 6, which depicts a heap as organized into three generations 58, 60, and 62. Assume that generation 60 is to be collected. The process for this individual generation may be more or less the same as that described in connection with FIGS. 4 and 5 for the entire heap, with one major exception. In the case of a single generation, the root set must be considered to include not only the call stack, registers, and global variables represented by set 52 but also objects in the other generations 58 and 62, which themselves may contain references to objects in generation 60. So pointers must be traced not only from the basic root set 52 but also from objects within the other generations.
One could perform this tracing by simply inspecting all references in all other generations at the beginning of every collection cycle, and it turns out that this approach is actually feasible in some situations. But it takes too long in other situations, so workers in this field have employed a number of approaches to expediting reference tracing. One approach is to include so-called write barriers in the mutator process. A write barrier is code added to a write operation to record information from which the collector can determine where references were or may have been written since the last collection cycle. A reference list can then be maintained by taking such a list as it existed at the end of the previous collection cycle and updating it by inspecting only locations identified by the write barrier as possibly modified since the last collection cycle.
One of the many such implementations commonly used by workers in this art employs what has been referred to as the xe2x80x9ccard table.xe2x80x9d FIG. 6 depicts the various generations as being divided into smaller sections, known for this purpose as xe2x80x9ccards.xe2x80x9d Card tables 64, 66, and 68 associated with respective generations contain an entry for each of their cards. Each card-table entry includes some summary of its associated card""s inter-generational-reference content. This summary may be no more than a binary indication of whether such a reference exists, but it preferably includes a list of offsets that indicate such references"" locations in the card. Part of entry 69, for instance, may be an offset that indicates the location of an inter-generational reference 70 within the corresponding card 72. For the sake of concreteness, we will assume that the summaries are maintained by steps that occur principally at the beginning of each collection cycle.
Of course, the card-table approach is only one of many that can be employed to detect inter-generational pointers. Indeed, it is typical for an individual garbage collector to use more than one approach. Although there is no reason in principle to favor any particular number of generations, and although FIG. 6 shows three, most generational garbage collectors have only two generations, of which one is the young generation and the other is the mature generation. Moreover, although FIG. 6 shows the generations as being of the same size, a more-typical configuration is for the young generation to be considerably smaller. Finally, although we assumed for the sake of simplicity that collection during a given cycle was limited to only one generation, a more-typical approach is actually to collect the whole young generation at every cycle but to collect the mature one less frequently.
To collect the young generation, it is preferable to employ the card table to identify pointers into the young generation; laboriously scanning the entire mature generation would take too long. On the other hand, since the young generation is collected in every cycle and can therefore be collected before mature-generation processing, it takes little time to scan the few remaining, live objects in the young generation for pointers into the mature generation in order to process that generation. For this reason, the card table will typically be so maintained as only to identify the regions occupied by references into younger generations and not into older ones.
Now, although it typically takes very little time to collect the young generation, it may take more time than is acceptable within a single garbage-collection cycle to collect the entire mature generation. So some garbage collectors may collect the mature generation incrementally; that is, they may perform only a part of the mature generation""s collection during any particular collection cycle. Incremental collection presents the problem that, since the generation""s objects that are outside a collection cycle""s collection set are not processed during that cycle, any such objects that are unreachable are not recognized as unreachable, so collection-set objects to which they refer tend not to be, either.
To reduce the adverse effect this would otherwise have on collection efficiency, workers in this field have employed the xe2x80x9ctrain algorithm,xe2x80x9d which FIG. 7 depicts. A generation to be collected incrementally is divided into sections, which for reasons about to be described are referred to as xe2x80x9ccar sections.xe2x80x9d The generation may be collected incrementally in fixed-size sections, in which case the size may equal that of the generation portion to be collected during one cycle. More typically, the fraction of the generation to be collected during any one cycle varies with run-time conditions, in which case the car-section size is often that of the minimum fraction to be collected in one cycle, although that is not a requirement.
The discussion that follows will occasionally follow the nomenclature in the literature by using the term car instead of car section. But the literature seems to use that term to refer variously not only to memory sections themselves but also to data structures that the train algorithm employs to manage them when they contain objects, as well as to the more-abstract concept that the car section and managing data structure represent in discussions of the algorithm. So the following discussion will more frequently use the expression car section to emphasize the actual sections of memory space for whose management the car concept is employed.
Additionally, the car sections are grouped into xe2x80x9ctrains,xe2x80x9d which are ordered according to age. For example, FIG. 7 shows an oldest train 73 consisting of a generation 74""s three car sections described by associated data structures 75, 76, and 78, while a second train 80 consists only of a single car section, represented by structure 82, and the youngest train 84 (referred to as the xe2x80x9callocation trainxe2x80x9d) consists of car sections that data structures 86 and 88 represent. As will be seen below, car sections"" train memberships can change, and any car section added to a train is added to the end of a train. Train size is a matter of design choice, but its purpose is to maximize the probability that garbage reference xe2x80x9ccyclesxe2x80x9d can be reclaimed, as will now be explained.
Although more than one car section may be collected in any given cycle, little generality is lost by considering the situation in which only a single car is collected during any cycle. The car collected in any cycle is the one added least recently to the oldest train. In FIG. 7, that car would be car 75. All of the generation""s cars can thus be thought of as waiting for collection in a single long line, in which cars in a given train are farther forward than those in any younger train, and those added to a train more recently are behind those added to it earlier.
As is usual, the way in which reachable objects are identified is to determine whether there are references to them in the root set or in any other object already determined to be reachable. In accordance with the train algorithm, the collector additionally performs a test to determine whether there are any references at all from outside the oldest train to objects within it. If there are none, then all cars within the train can be reclaimed, even though not all of those cars are in the collection set. And the train algorithm so operates that inter-car references tend to be grouped into trains, as will now be explained.
To identify references into the car from outside of it, train-algorithm implementations typically employ xe2x80x9cremembered sets.xe2x80x9d As card tables are, remembered sets are used to keep track of references. Whereas a card-table entry contains information about references that the associated card contains, though, a remembered set associated with a given region contains information about references into that region from locations outside of it. In the case of the train algorithm, remembered sets are associated with car sections. Each remembered set, such as car 75""s remembered set 90, lists locations in the generation that contain references into the associated car section. The remembered sets for all of a generation""s cars are typically updated at the start of each collection cycle, concurrently with card-table updates. For reasons that will become apparent, the collector will typically not bother to place in the remembered set the locations of references from objects in car sections farther forward in the collection queue, i.e., from objects in older trains or cars added earlier to the same train. For the sake of simplicity, we will continue the assumption that only a single car is collected during each collection cycle, although we will discuss multiple-car collection sets presently.
When the remembered sets have been updated, the collector reads the remembered set of each car in the collection set to determine the location of each reference from a higher-order car into the collection set. The collector places the address of each reference thereby found into a scratch-pad list associated with the train that contains that reference. (Again, the remembered set lists only reference locations in the same generation.)
When the collector has read all references in the remembered set, it evacuates into the youngest train the collection-set-car objects referred to by the references in the locations that the youngest train""s scratch-pad-list entries specify It also removes those scratch-pad-list entries and updates the references to which they pointed so that those references reflect the evacuated objects"" new locations. Any collection-set objects to which the thus-evacuated objects refer are similarly evacuated to that train, and this continues until that train no longer contains any references into the collection-set car section.
Whenever an object is evacuated, the collector leaves an indication of this fact in the object""s previous location, together with the address of its new location. So, if the reference found in the location identified by any subsequent scratch-pad-list entry refers to an already-evacuated object, the collector is apprised of this fact and can update the reference without attempting to evacuate the already-evacuated object.
This process is repeated for successively older trains until the collector reaches the oldest train. Before it processes references in that train""s scratch-pad list,the collector evacuates any objects referred to from outside the generation To identify such objects, the collector scans the root set and other generations for references into the collection set. Now, it may not be necessary to scan all other generations. A particularly common scheme is not to collect any generation in a collection cycle in which every younger generation is not completely collected, and the policy may be to promote all surviving younger-generation objects into older generations. In such a situation, it is necessary only to scan older generations.
The scanning may actually involve inspecting each surviving object in the other generation, or the collector may expedite the process by using card-table entries. Regardless of which approach it uses, the collector immediately evacuates into another train any collection-set object to which it thereby finds an external reference. The typical policy is to place the evacuated object into the youngest such train. As before, the collector does not attempt to evacuate an object that has already been evacuated, and, when it does evacuate an object to a train, it evacuates to the same train any objects in the collection-set car to which the thus-evacuated object refers. In any case, the collector updates the reference to the evacuated object.
When all inter-generationally referred-to objects have been evacuated from the collection-set car the garbage collector then performs the train algorithm""s central test: it determines whether there are any references into the oldest train from outside that train. If there are none other than those in the collection set, the entire train can be reclaimed, even if there are inter-car references between its individual cars. By evacuating objects into the trains that references to them occupy, the train algorithm tends to group garbage reference cycles into single trains, whose sizes are not limited, as car sizes are, by the need to optimize collection-cycle duration. The train algorithm is thus able to reclaim space occupied by large garbage reference cycles even if the space increments (car sections) that it collects are relatively small. To support this process, a tally of how many references there are from other trains in the same generation is typically maintained in connection with the various remembered-set updates. This tally, together with a tally of extra-generational references developed during the extra-generational scan, yields the indications of whether there are any references into a given train from outside that train.
If references into the oldest train have prevented its car sections from being reclaimed, the collector proceeds to evacuate any collection-set objects referred to by references whose locations the oldest train""s scratch-pad list includes. It removes them to younger cars in the oldest train, again updating references, avoiding duplicate evacuations, and evacuating any collection-set-car objects to which the evacuated objects refer. When this process has been completed, the collection set can be reclaimed, since any reference to any remaining object must reside in the same car, so all remaining collection-set objects are unreachable.
FIGS. 8A-8J illustrate results of using the train algorithm. FIG. 8A represents a generation in which objects have been allocated in nine car sections. The oldest train has four cars, numbered 1.1 through 1.4. Car 1.1 has two objects, A and B. There is a reference to object B in the root set (which, as was explained above, includes live objects in the other generations). Object A is referred to by object L, which is in the third train""s sole car section. In the generation""s remembered sets 92, a reference in object L has therefore been recorded against car 1.1.
Processing always starts with the oldest train""s earliest-added car, so the garbage collector refers to car 1.1""s remembered set and finds that there is a reference from object L into the car being processed. It accordingly evacuates object A to the train that object L occupies. The object being evacuated is often placed in one of the selected train""s existing cars, but we will assume for present purposes that there is not enough room. So the garbage collector evacuates object A into a new car section and updates appropriate data structures to identify it as the next car in the third train. FIG. 8B depicts the result: a new car has been added to the third train, and object A is placed in it.
FIG. 8B also shows that object B has been evacuated to a new car outside the first train. This is because object B has an external reference, which, like the reference to object A, is a reference from outside the first train, and one goal of the processing is to form trains into which there are no further references. Note that, to maintain a reference to the same object, object L""s reference to object A has had to be rewritten, and so have object B""s reference to object A and the inter-generational pointer to object B. In the illustrated example, the garbage collector begins a new train for the car into which object B is evacuated, but this is not a necessary requirement of the train algorithm. That algorithm requires only that externally referenced objects be evacuated to a newer train.
Since car 1.1 no longer contains live objects, it can be reclaimed, as FIG. 8B also indicates. Also note that the remembered set for car 2.1 now includes the address of a reference in object A, whereas it did not before. As was stated before, remembered sets in the illustrated embodiment include only references from cars further back in the order than the one with which the remembered set is associated. The reason for this is that any other cars will already be reclaimed by the time the car associated with that remembered set is processed, so there is no reason to keep track of references from them.
The next step is to process the next car, the one whose index is 1.2. The purpose of a generation""s division into car sections is to provide for incremental collection, so collection of car 1.2 can wait until the collection cycle following the one during which car 1.1 is collected. Indeed, the garbage collector can postpone its processing for several collection cycles (during which presumably only younger generations are collected). As was mentioned above, we will assume in this scenario that each car""s collection occurs in its own separate collection cycle. And for the sake of simplicity we will assume that the mutator does not change any references into the generation in the interim.
FIG. 8B depicts car 1.2 as containing only a single object, object C, and that car""s remembered set contains the address of an inter-car reference from object F. The garbage collector follows that reference to object C. Since this identifies object C as possibly reachable, the garbage collector evacuates it from car set 1.2, which is to be reclaimed. Specifically, the garbage collector removes object C to a new car section, section 1.5, which is linked to the train to which the referring object F""s car belongs. Of course, object F""s reference needs to be updated to object C""s new location. FIG. 8C depicts the evacuation""s result.
FIG. 8C also indicates that car set 1.2 has been reclaimed, and car 1.3 is next to be processed. The only address in car 1.3""s remembered set is that of a reference in object G. Inspection of that reference reveals that it refers to object F. Object F may therefore be reachable, so it must be evacuated before car section 1.3 is reclaimed. On the other hand, there are no references to objects D and E, so they are clearly garbage. FIG. 8D depicts the result of reclaiming car 1.3""s space after evacuating possibly reachable object F.
In the state that FIG. 8D depicts, car 1.4 is next to be processed, and its remembered set contains the addresses of references in objects K and C. Inspection of object K""s reference reveals that it refers to object H, so object H must be evacuated. Inspection of the other remembered-set entry, the reference in object C, reveals that it refers to object G, so that object is evacuated, too. As FIG. 8E illustrates, object H must be added to the second train, to which its referring object K belongs. In this case there is room enough in car 2.2, which its referring object K occupies, so evacuation of object H does not require that object K""s reference to object H be added to car 2.2""s remembered set. Object G is evacuated to a new car in the same train, since that train is where referring object C resides. And the address of the reference in object G to object C is added to car 1.5""s remembered set.
FIG. 8E shows that this processing has eliminated all references into the first train, and it is an important part of the train algorithm to test for this condition. That is, even though there are references into both of the train""s cars, those cars"" contents can be recognized as all garbage because there are no references into the train from outside of it. So all of the first train""s cars are reclaimed.
The collector accordingly processes car 2.1 during the next collection cycle, and that car""s remembered set indicates that there are two references outside the car that refer to objects within it. Those references are in object K, which is in the same train, and object A, which is not. Inspection of those references reveals that they refer to objects I and J, which are evacuated.
The result, depicted in FIG. 8F, is that the remembered sets for the cars in the second train reveal no inter-car references, and there are no inter-generational references into it, either. That train""s car sections therefore contain only garbage, and their memory space can be reclaimed.
So car 3.1 is processed next. Its sole object, object L, is referred to inter-generationally as well as by a reference in the fourth train""s object M. As FIG. 8G shows, object L is therefore evacuated to the fourth train. And the address of the reference in object L to object A is placed in the remembered set associated with car 3.2, in which object A resides.
The next car to be processed is car 3.2, whose remembered set includes the addresses of references into it from objects B and L. Inspection of the reference from object B reveals that it refers to object A, which must therefore be evacuated to the fifth train before car 3.2 can be reclaimed. Also, we assume that object A cannot fit in car section 5.1, so a new car 5.2 is added to that train, as FIG. 8H shows, and object A is placed in its car section. All referred-to objects in the third train having been evacuated, that (single-car) train can be reclaimed in its entirety.
A further observation needs to be made before we leave FIG. 8G. Car 3.2""s remembered set additionally lists a reference in object L, so the garbage collector inspects that reference and finds that it points to the location previously occupied by object A. This brings up a feature of copying-collection techniques such as the typical train-algorithm implementation. When the garbage collector evacuates an object from a car section, it marks the location as having been evacuated and leaves the address of the object""s new location. So when the garbage collector traces the reference from object L, it finds that object A has been removed, and it accordingly copies the new location into object L as the new value of its reference to object A.
In the state that FIG. 8H illustrates, car 4.1 is the next to be processed. Inspection of the fourth train""s remembered sets reveals no inter-train references into it, but the inter-generational scan (possibly performed with the aid of FIG. 6""s card tables) reveals inter-generational references into car 4.2. So the fourth train cannot be reclaimed yet. The garbage collector accordingly evacuates car 4.1""s referred-to objects in the normal manner, with the result that FIG. 8I depicts.
In that state, the next car to be processed has only inter-generational references into it. So, although its referred-to objects must therefore be evacuated from the train, they can be placed in any other train. In the illustrated implementation, a new train is formed for this purpose, so the result of car 4.2""s processing is the state that FIG. 8J depicts.
Processing continues in this same fashion. Of course, subsequent collection cycles will not in general proceed, as in the illustrated cycles, without any reference changes by the mutator and without any addition of further objects. But reflection reveals that the general approach just described still applies when such mutations occur.
One shortcoming of the conventional approach to train-algorithm-based garbage collection is that certain types of objects will not end up being identified as unreachable unless measures are taken that compromise the train algorithm""s incremental nature. To appreciate this, consider FIGS. 9A and 9B, which illustrate a typical approach to memory allocation for class information.
In an object-oriented program, a typical object 100 (FIG. 9B) contains a reference to information about the class to which it belongs. In a dynamically loaded system, such information is often loaded in units of xe2x80x9cclass files,xe2x80x9d of which the drawing gives an example 102 (FIG. 9A). The drawing depicts class file 102 as being stored outside of the train-algorithm-managed heap 104 (FIG. 9B). This is typical because the collection procedure uses the class file""s information about object sizes, etc., and relocating the source of that information as part of the relocation process that depends on it would be complicated. But thus locating such class information outside the train-algorithm-managed heap tends to make it hard to collect some types of objects in an incremental fashion.
To appreciate this, it is helpful to consider a typical organization of class information. It turns out that the class file""s organization may not be optimum for a given run-time system to use. So some of that information and/or information derived from it may be placed in a more-convenient form. To indicate this, the drawings show a xe2x80x9cnearClassxe2x80x9d structure 106. Its location may be used by object headers to identify the classes to which their objects belong, as the drawing indicates by pointer 107 in object 100""s header. Because of such uses, the nearClass structure, too, is located outside of the heap 104.
But not all of the class-related information is located outside the heap 104. In the case of the Java programming language, for instance, it is a requirement that one be able to refer to a class as an object, so there must be an object structure to represent it. Compilers that target the Java virtual machine employ objects of the java.lang.Class class, which the drawing exemplifies in structure 108. Such objects, which we refer to herein as xe2x80x9cproxy objectsxe2x80x9d because of a use to which our invention""s below-illustrated embodiment puts them, should be located in the heap, as other objects are.
Now, the process of collecting structure 108 presents difficulties because of the types of references to it there tend to be. The class structure 102 itself will typically include a reference 110 to it. It will also be ultimately reachable from a xe2x80x9cclass loader.xe2x80x9d A class loader is a structure, exemplified in the drawings by structure 112, that contains information about how to load a class, such as where to look for its class file. For example, the virtual machine looking for xe2x80x9courFirm.MyClassxe2x80x9d may look in the local hard disk if the default loader is specified but instead look to a remote server if a different loader is specified.
The memory allocated to such a structure 112 cannot be reclaimed until the classes loaded in accordance with its contents are no longer reachable, so it contains a list of the classes it has been used to load. The illustrated embodiment implements this feature by a reference 114 to a vector structure 116. In the illustrated embodiment, this structure includes metadata about the vector, including a pointer 118 to structures such as structure 120 that contain the actual list of references to the classes that the loader has loaded. If the class represented by proxy object 108 has been loaded by the loader that structure 112 represents, then the list-containing structure 120 will include a reference 122 to the proxy object 108.
The problem that references of these types cause is that they can form cycles of references that the conventional train-algorithm implementation does not concentrate. As the diagram illustrates, an object such as object 100 of a given class refers to that class""s nearClass structure 106, which refers to the class structure 102, which points to the proxy object 108. In the conventional train-algorithm approach, such external references tend to make the proxy object appear reachable, even though it may actually be part of an unreachable cycle, because the cycle runs through external space.
One can identify such cycles by periodically performing special, whole-heap (non-incremental) collection intervals. But this is unworkable for an application that cannot tolerate the delays that non-incremental collection often imposes. Alternatively, one can simply leave proxy objects uncollected. But this approach is unacceptable for applications that generate large numbers of classes.
We have devised a way of identifying such unreachable cycles, and reclaiming the associated memory resources, as part of the train algorithm""s incremental collection process. Our approach involves associating various xe2x80x9cproxyxe2x80x9d objects in the train-managed heap with respective resources outside of it. If all external references to a collection-set object are located in resources associated with proxies in the heap, that object is, in essence, evacuated to the youngest train containing such a proxy, not the youngest train overall. So objects involved in cycles that include external objects can, like those involved in cycles entirely within the heap, be concentrated in single trains so that their resources are reclaimed by incremental collection. The external resources in the cycle can be reclaimed at the same time.